package com.sunlee.admin.api.module.oss.cloud.impl;

import cn.hutool.core.util.ObjectUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.sunlee.admin.api.module.oss.cloud.AbstractCloudStorageService;
import com.sunlee.admin.api.module.oss.cloud.CloudStorageConfig;
import io.github.artislong.core.StandardOssClient;
import io.github.artislong.core.ali.AliOssClient;
import io.github.artislong.core.ali.AliOssProperties;
import io.github.artislong.core.ali.model.AliOssClientConfig;
import io.github.artislong.core.ali.model.AliOssConfig;
import io.github.artislong.model.OssInfo;

import java.io.InputStream;
import java.util.Optional;

/**
 * 阿里云存储
 *
 * @author sunlee
 */

public class ALiYunCloudStorageService extends AbstractCloudStorageService {

    private AliOssClient aliOssClient;

    /**
     * 通过构造器初始化
     *
     * @param config 公共配置
     */
    public ALiYunCloudStorageService(CloudStorageConfig config) {
        this.config = config;
        this.init(buildOssClient());
    }

    /**
     * 构建一个ALiOss客户端
     *
     * @return ALiOss客户端
     */
    public StandardOssClient buildOssClient() {

        AliOssProperties aliOssProperties = new AliOssProperties();

        aliOssProperties.setEnable(true);
        aliOssProperties.setBasePath("/");
        aliOssProperties.setBucketName(config.getAliyunBucketName());
        aliOssProperties.setEndpoint(config.getAliyunEndPoint());
        aliOssProperties.setAccessKeyId(config.getAliyunAccessKeyId());
        aliOssProperties.setAccessKeySecret(config.getAliyunAccessKeySecret());

        aliOssClient = new AliOssClient(ossClient(aliOssProperties), aliOssProperties);
        return aliOssClient;
    }

    private OSS ossClient(AliOssConfig aliOssConfig) {
        String securityToken = aliOssConfig.getSecurityToken();
        AliOssClientConfig clientConfiguration = aliOssConfig.getClientConfig();
        if (ObjectUtil.isEmpty(securityToken) && ObjectUtil.isNotEmpty(clientConfiguration)) {
            return new OSSClientBuilder().build(aliOssConfig.getEndpoint(),
                    aliOssConfig.getAccessKeyId(),
                    aliOssConfig.getAccessKeySecret(), clientConfiguration.toClientConfig());
        }
        if (ObjectUtil.isNotEmpty(securityToken) && ObjectUtil.isEmpty(clientConfiguration)) {
            return new OSSClientBuilder().build(aliOssConfig.getEndpoint(),
                    aliOssConfig.getAccessKeyId(),
                    aliOssConfig.getAccessKeySecret(), securityToken);
        }
        return new OSSClientBuilder().build(aliOssConfig.getEndpoint(),
                aliOssConfig.getAccessKeyId(),
                aliOssConfig.getAccessKeySecret(), securityToken,
                Optional.ofNullable(clientConfiguration).orElse(new AliOssClientConfig()).toClientConfig());
    }

    @Override
    public OssInfo upLoad(InputStream is, String targetName) {
        OssInfo ossInfo = super.upLoad(is, targetName);
        ossInfo.setPath(config.getAliyunDomain() + ossInfo.getPath());
        return ossInfo;
    }

    @Override
    public void delete(String targetName) {
        super.delete(targetName.replace(config.getAliyunDomain(), ""));
    }

    @Override
    public Boolean isExist(String targetName) {
        return aliOssClient.isExist(targetName.replace(config.getAliyunDomain(), ""));
    }
}